
from saas.log import *
import sys



class conf:

	## option defaults
	client_secret      = 'abc'
	ipv4_bind_addr     = '0.0.0.0'
	ipv6_bind_addr     = '::'
	db_path            = ''
	port               = 1234
	use_ipv4           = 1
	use_ipv6           = 1
	enc_type           = SAAS_ENC_TYPE_AES256CBC_SHA256
	time_adjust        = 0
	password_salt      = 'ghi'
	passwd_chg_policy  = 3
	key_time_type      = 'sec'
	key_time_tolerance = 1


	def parse_enc_type( self , enc_type ):
		if enc_type.lower() == 'aes256cbc-sha256':
			logm( LOG_INFO , 'using encryption type AES256CBC_SHA256' )
			return SAAS_ENC_TYPE_AES256CBC_SHA256
		elif enc_type.lower() == 'aes256ecb-sha256':
			logm( LOG_INFO , 'using encryption type AES256ECB_SHA256' )
			return SAAS_ENC_TYPE_AES256ECB_SHA256
		elif enc_type.lower() == 'aes128cbc-sha256xor':
			logm( LOG_INFO , 'using encryption type AES128CBC_SHA256XOR' )
			return SAAS_ENC_TYPE_AES128CBC_SHA256XOR
		elif enc_type.lower() == 'aes128ecb-sha256xor':
			logm( LOG_INFO , 'using encryption type AES128ECB_SHA256XOR' )
			return SAAS_ENC_TYPE_AES128ECB_SHA256XOR
		else:
			logm( LOG_ERR , 'Cannot parse encryption type `%s\' -- unknown encryption type' % ( enc_type ) )
			sys.exit( 1 )


	def dump( self ):
		logm( LOG_DEBUG , ' - client_secret      = %s' % ( self.client_secret      ) )
		logm( LOG_DEBUG , ' - ipv4_bind_addr     = %s' % ( self.ipv4_bind_addr     ) )
		logm( LOG_DEBUG , ' - ipv6_bind_addr     = %s' % ( self.ipv6_bind_addr     ) )
		logm( LOG_DEBUG , ' - db_path            = %s' % ( self.db_path            ) )
		logm( LOG_DEBUG , ' - port               = %d' % ( self.port               ) )
		logm( LOG_DEBUG , ' - use_ipv4           = %d' % ( self.use_ipv4           ) )
		logm( LOG_DEBUG , ' - use_ipv6           = %d' % ( self.use_ipv6           ) )
		logm( LOG_DEBUG , ' - time_adjust        = %d' % ( self.time_adjust        ) )
		logm( LOG_DEBUG , ' - password_salt      = %s' % ( self.password_salt      ) )
		logm( LOG_DEBUG , ' - passwd_chg_policy  = %d' % ( self.passwd_chg_policy  ) )
		logm( LOG_DEBUG , ' - key_time_type      = %s' % ( self.key_time_type      ) )
		logm( LOG_DEBUG , ' - key_time_tolerance = %s' % ( self.key_time_tolerance ) )


	def parse( self , filename ):
		file = open( filename , 'r' )
		data = file.read()
		file.close()

		for line in data.splitlines():
			## strip out comments.
			if line.find( '#' ) != -1:
				pos = line.find( '#' )
				line = line[:pos]

			## if it has an = sign, split it.
			if line.find( '=' ) == -1:
				continue

			(key, value) = line.split( '=' )
			key = key.strip()
			value = value.strip()

			## if we have troubles parsing any of this shit, just ignore
			## it and move on.
#			try:
			if key == 'client_secret':
				self.client_secret = value
			elif key == 'ipv4_bind_addr':
				if value == 'all':
					self.ipv4_bind_addr = '0.0.0.0'
				else:
					self.ipv4_bind_addr = value
			elif key == 'ipv6_bind_addr':
				if value == 'all':
					self.ipv6_bind_addr = '::'
				else:
					self.ipv6_bind_addr = value
			elif key == 'db_path':
				self.db_path = value
			elif key == 'port':
				self.port = int( value )
			elif key == 'use_ipv4':
				self.use_ipv4 = int( value )
			elif key == 'use_ipv6':
				self.use_ipv6 = int( value )
			elif key == 'time_adjust':
				self.time_adjust = int( value )
			elif key == 'password_salt':
				self.password_salt = value
			elif key == 'passwd_chg_policy':
				self.passwd_chg_policy = int( value )
			elif key == 'key_time_type':
				self.key_time_type = value
			elif key == 'key_time_tolerance':
				self.key_time_tolerance = value
			elif key == 'enc_type':
				self.enc_type = self.parse_enc_type( value )
#			except:
#				print( 'exception' )
#				continue



